--  写出英雄文件
local function create_profession()
	package.path = package.path .. [[;D:\War3\the-second-project\scripts\ini_data\?.lua]]
	ac = {}
	ac.ini_data = {}
	local all_data = require '战士'

	local char = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	local len = #char
	local function get_id( index )
		index = index - 1
		local a = math.floor( index / len )
		local b = index - a * len
		a = a + 1
		b = b + 1
		return 'H0' .. char:sub(a, a) .. char:sub(b, b)
	end

	local output_path = fs.path(CURRENT_PATH .. '\\output')

	local hero_str = [=[
	ac.hero['%s']{
		war3_id = '%s',
		
		attribute = {
			move_speed = 300,
			--基础护甲
			defence = 0.0,
			--攻击数据只需要攻击1即可
			attack = 0,
			--骰子面数
			attack_sides = 1,
			--骰子数量
			attack_dice = 1,
			attack_cool = 0.8,
			attack_range = 128,
			mana = 300,
			mana_recover = 0.5,
			life = 100,
			life_recover = 0.0,
			str = 0,
			agi = 0,
			int = 0,
		},

		is_buyer = true,

		--升级数据
		upgrade = {
			str = 35,
			agi = 15,
			int = 3,
		},
		
	}
	]=]

	local skill_tip = [=[
	local mt = ac.skill['%s']
	mt{
		tip = [[
	%s
	]],

	}

	function mt:on_enable()
		local unit = self.owner
	end

	function mt:on_disable()
		local unit = self.owner
	end
	]=]

	local init_str = [=[
	reload 'hero.hero.%s.%s'
	reload 'hero.hero.%s.技能'
	]=]

	local unit_str = [=[
		--%s
		{
			--id
			war3_id = '%s',
			
			--名字
			name = '%s',
			--描述
			description = '%s',

			file = [[units\human\Footman\Footman.blp]],
			art = [[ReplaceableTextures\CommandButtons\BTNFootman.blp]],
		},
	]=]

	local reload_str = [=[reload 'hero.hero.%s._init']=]

	local all_reloads = {}
	for i = 1, #all_data do
		local id = get_id(i+2)
		local data = all_data[i]
		local dir = output_path / data['职业名称']
		fs.create_directory(dir)

		local file = io.open( ('%s\\output\\%s\\%s.lua'):format(CURRENT_PATH, data['职业名称'], data['职业名称']), 'w' )
		local s = hero_str:format( data['职业名称'], id )
		file:write(s)
		file:close()

		local file = io.open( ('%s\\output\\%s\\技能.lua'):format(CURRENT_PATH, data['职业名称']), 'w' )
		local s = skill_tip:format( data['技能'], data['技能描述'] )
		file:write(s)
		file:close()

		local file = io.open( ('%s\\output\\%s\\_init.lua'):format(CURRENT_PATH, data['职业名称']), 'w' )
		local s = init_str:format( data['职业名称'], data['职业名称'], data['职业名称'] )
		file:write( s )
		file:close()

		log.info(unit_str:format(data['职业名称'], id, data['职业名称'], data['职业名称']))

		all_reloads[#all_reloads+1] = reload_str:format(data['职业名称'])
	end
	log.info(table.concat(all_reloads, '\n'))
end

local function duel_doo()
	local INPUT_PATH = [[C:\Users\Administrator\Desktop\新建文件夹\代号 路径修改2.1\map\war3map.doo]]
	local INPUT_PATH = [[C:\Users\Administrator\Desktop\代号 路径修改3.0\map\war3map.doo]]
	local doo = Doodads.new( INPUT_PATH )
	doo:load()

	for normal_doo in doo:each('普通') do
		if normal_doo.position[3] > 0 then
			normal_doo.position[3] = normal_doo.position[3] - 256
		end
	end
	for normal_doo in doo:each('特殊') do
		if normal_doo.position[3] > 0 then
			normal_doo.position[3] = normal_doo.position[3] - 256
		end
	end

	local buffer = doo:save()
	local file = io.open( ('%s\\output\\war3map.doo'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()
end

-- 复制目标w3e的贴图信息到另一个w3e中
local function copy_w3e_tileset()

	-- 需要改变信息的w3e
	local INPUT_PATH = [[C:\Users\Administrator\Desktop\地形\FYZB\map\war3map.w3e]]
	print('path:', INPUT_PATH)
	local w3e = W3e.new( INPUT_PATH )
	w3e:load()

	-- 目标w3e
	local INPUT_PATH = [[C:\Users\Administrator\Desktop\地形\狩猎计划\map\war3map.w3e]]
	local w3e2 = W3e.new( INPUT_PATH )
	w3e2:load()

	w3e.tileset = w3e2.tileset
	w3e.custom_tileset = w3e2.custom_tileset
	w3e.tileset_texture_count = w3e2.tileset_texture_count
	w3e.tileset_ids = w3e2.tileset_ids
	w3e.cliffset_texture_count = w3e2.cliffset_texture_count
	w3e.cliffset_ids = w3e2.cliffset_ids


	local buffer = w3e:save()
	local file = io.open( ('%s\\output\\war3map.w3e'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()

	-- local types = w3e:write_terrain()
	
	-- local file = io.open( ('%s\\output\\w3e.jass'):format(CURRENT_PATH), 'w' )

	-- for i = 1, #types do
	--     for j = 1, #types[i] do
	--         local data = types[i][j]
	--         file:write( ([[call SetTerrainType( %s, %s, '%s', - 1, 1, 1)]]):format(data.x, data.y, data.type) )
	--         file:write('\n')
	--     end
	-- end

	-- file:close()
end

local function duel_unitdoo()
	print(INPUT_PATH)
	local doos = UnitDoodads.new( INPUT_PATH )
	doos:load()
end

local function duel_binary()
	local binary_writer = Binary_writer.new()
	binary_writer:write('d', 1)
	local buffer = binary_writer:file_write()
	 local file = io.open( ('%s\\tt.doo'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()
	-- local file = io.open( ('%s\\tt.doo'):format(CURRENT_PATH), 'rb' )
	-- local filestr = file:read '*a'
	-- file:close()
	-- local binary_reader = Binary_reader.new(filestr)
	-- print(binary_reader:read 'f')
end


--  左上角x，y, 长
local get_area = function ( x, y, range )
	local minx = x
	local miny = y - range
	local maxx = x + range
	local maxy = y

	return minx, miny, maxx, maxy
end

local function copy_doo()
	local path = [[D:\War3\the-second-project\map\war3map.doo]]
	local path2 = [[D:\War3\the-fourth-project\map\war3map.doo]]
	
	local new_doo = Doodads.new( path2 )
	new_doo:load()
	
	local doo = Doodads.new( path )
	doo:load()
	
	local correct_x = -11*512 - (-7*512)
	local correct_y = 11*512 - 15*512

	local correct_x2 = 4*512 - (-7*512)
	local correct_y2 = 11*512 - 15*512

	local correct_x3 = -11*512 - (-7*512)
	local correct_y3 = -4*512 - 15*512

	local correct_x4 = 4*512 - (-7*512)
	local correct_y4 = -4*512 - 15*512
	
	local correct_xs = {
		correct_x, correct_x2, correct_x3, correct_x4
	}
	local correct_ys = {
		correct_y, correct_y2, correct_y3, correct_y4,
	}
	
	local minx, miny, maxx, maxy = get_area( -7*512, 15*512, 512*7 )
	print( get_area( -7*512, 15*512, 512*7 ) )
	for _, normal_doo in ipairs(doo.normal_doos) do
		local x, y = normal_doo.position[1], normal_doo.position[2]
		if x>=minx and x<=maxx and y>=miny and y<=maxy then
			
			for i = 1, 4 do
				local new_nor = table.deepcopy(normal_doo)
				new_nor.position[1] = new_nor.position[1] + correct_xs[i]
				new_nor.position[2] = new_nor.position[2] + correct_ys[i]

				new_doo.normal_doos[#new_doo.normal_doos+1] = new_nor
			end

		end
	end

	local buffer = new_doo:save()
	local file = io.open( ('%s\\output\\war3map.doo'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()
end

local spread_over_doo = function ()
	local path = [[D:\War3\the-fourth-project\map\war3map.doo]]
	local doo = Doodads.new( path )
	doo:load()

	local path2 = [[D:\War3\the-fourth-project\map\war3map.w3e]]
	local w3e = W3e.new( path2 )
	w3e:load()

	local width, height = w3e.width, w3e.height
	local startx = w3e.offset[1]
	local starty = w3e.offset[2] + (height-1)*128

	local max_col = math.ceil(width/5)
	local max_row = math.ceil(height/5)
	
	for row = 1, max_row do
		for col = 1, max_col do
			local x, y
			if col == max_col then
				x = (max_col-1-2.5)*512 + startx
			else
				x = (col-1+2.5)*512 + startx
			end
			if row == max_row then
				y = starty-(max_row-1-2.5)*512
			else
				y = starty-(row-1+2.5)*512
			end
			
			doo.normal_doos[#doo.normal_doos+1] = {
				id = 'D044',
				variation = 0,
				position = {
					x, y, -256-256-128,
				},
				angle = 0,
				scale = {
					14.5,
					17,
					1,
				},
				state = 6,
				life = 255,
				drop_item_pointer = -1,
				drop_item_set = {},
				creation_number = #doo.normal_doos+1,
			}
		end
	end

	local buffer = doo:save()
	local file = io.open( ('%s\\output\\war3map.doo'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()
end

local duel_modellib = function ()
	local lib = require 'script.modellib.init'
	local uni = require 'script.map.unicode'

	local list = Mpq.w2l.input_ar
	local buf = list:get('resource\\[ake] 173.mdx')

	local path = 'resource\\[ake] 173.mdx'
	local name = path

	local filename = Md5(buf) .. '.mdx'
	local model = lib.model.open_by_memory(name, buf)
	if model == nil then 
		return 
	end 
	local test = lib.model.open_by_memory(name, model:save_to_memory(filename))
	if test == nil then 
		return 
	end 
	
	local bone_parent = {}
	for bone in model:each_bone() do 
		bone_parent[bone.parent_id] = bone
	end 
	--遍历丝带 如果丝带上面绑定了骨骼 则该模型是必崩的模型
	for ribbon in model:each_ribbon() do 
		if bone_parent[ribbon.object_id] then 
			print('跳过崩溃模型', filename)
			return 
		end 
	end 

	--遍历动画 如果开始帧跟结束帧是一样的 删除该动画
	for sequence in model:each_sequence() do 
		if sequence.interval.x == sequence.interval.y then 
			sequence.interval.y = sequence.interval.y + 1
			print('修正错误帧动画', filename, uni.a2u(sequence.name))
		end 
	end 

	model:close()

	print(model, filename, test)

end

-- 修改doo和doo.ini的id
-- 移除没有在地形中使用到的doodad物编信息
local change_doo_id = function ()
	local lni = require 'lni'
	local origin_ini = ('%s\\table\\doodad.ini'):format(INPUT_PATH)
	local file = io.open(origin_ini, 'r')
	local content = file:read('a')
	file:close()
	local result = lni.classics(content)
	local lni_ids = {}
	for id in pairs(result) do
		lni_ids[id] = true
	end

	local path = ('%s\\map\\war3map.doo'):format(INPUT_PATH)
	local out_path = ('%s\\output\\doodad.ini'):format(CURRENT_PATH)
	local doo = Doodads.new( path )
	doo:load()
	local ids = {}
	local new_normal_doos = {}
	for _, obj in ipairs(doo.normal_doos) do
		if lni_ids[obj.id] then
			if true then
				local new_id
				if obj.id:sub(1, 2) == 'D0' then
					new_id = ('D4%s'):format(obj.id:sub(3))
				else
					new_id = obj.id
				end
				ids[obj.id] = new_id
	
				local new_obj = table.deepcopy(obj)
				new_obj.id = new_id
				new_normal_doos[#new_normal_doos+1] = new_obj
			else
				local new_id = obj.id
				ids[obj.id] = new_id
				local new_obj = table.deepcopy(obj)
				new_obj.id = new_id
				new_normal_doos[#new_normal_doos+1] = new_obj
			end
		end
	end
	local data = Mpq.w2l.slk['doodad']
	local datas = {}
	for k, v in pairs(data) do
		if ids[k] then
			datas[ ids[k] ] = v
		end
	end

	Mpq.w2l.slk['doodad'] = datas
	local content = Mpq.w2l:backend_lni('doodad', datas)
	
	local file = io.open(out_path, 'w')
	file:write(content)
	file:close()

	doo.normal_doos = new_normal_doos
	local buffer = doo:save()
	local file = io.open( ('%s\\output\\war3map.doo'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()

end

-- 给地形添加阻隔器
local add_barrier = function ()
	local path = [[C:\war3\the-sixth-project\map\war3map.w3e]]
	local w3e = W3e.new( path )
	w3e:load()
	

end

-- 让目标装饰物对称一下
local symmetry_target_doo = function ()
	local path = [[C:\Users\Administrator\Desktop\FYZB\map\war3map.doo]]
	local doo = Doodads.new( path )
	doo:load()
	local position
	for _, obj in ipairs(doo.normal_doos) do
		if obj.id == 'D000' then
			if obj.position[1] < 0 and obj.position[2] > 0 then
				position = table.deepcopy(obj.position)
				break
			end
		end
	end

	if position then
		for _, obj in ipairs(doo.normal_doos) do
			if obj.id == 'D000' then
				if obj.position[1] < 0 and obj.position[2] < 0 then
					obj.position = {
						position[1],
						-position[2],
						position[3],
					}
				end
			elseif obj.id == 'D00B' then
				if obj.position[1] > 0 and obj.position[2] > 0 then
					obj.position = {
						-position[1],
						position[2],
						position[3],
					}
				elseif obj.position[1] > 0 and obj.position[2] < 0 then
					obj.position = {
						-position[1],
						-position[2],
						position[3],
					}
				end
			end
		end

		local buffer = doo:save()
		local file = io.open( ('%s\\output\\war3map.doo'):format(CURRENT_PATH), 'wb' )
		file:write(buffer)
		file:close()
	else
		log.error('居然没找到')
	end
	
	
end

local daihao2 = function ()
	-- D000
	-- D00B
	local path = [[C:\Users\Administrator\Desktop\地形\地形0515\FYZB\map\war3map.doo]]
	local doo = Doodads.new( path )
	doo:load()
	local index = 0
	local index2 = 0
	for _, obj in ipairs(doo.normal_doos) do
		if obj.id == 'D000' and obj.position[2]<4000 then
			print(obj.position[1], obj.position[2])
			if index == 0 then
				obj.position[1] = -9.5*512
				obj.position[2] = 0
			elseif index == 1 then
				obj.position[1] = -9.5*512
				obj.position[2] = -10*512
			end
			index = index+1
		elseif obj.id == 'D00B' then
			if index2 == 0 then
				obj.position[1] = 2*512
				obj.position[2] = 0
			elseif index2 == 1 then
				obj.position[1] = 2*512
				obj.position[2] = -10*512
			end
			index2 = index2+1
		end
	end

	local buffer = doo:save()
	local file = io.open( ('%s\\output\\war3map.doo'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()
end

-- 代号战歌2处理模型镜头
local daihao2_hero_camera = function ()
	
	local lib = require 'script.modellib.init'

	local base_path = [[C:\war3\the-seventh-project\resource\]]
	local path = base_path .. [[ghoul.mdl]]

	local buf = list:get(path)
	local name = path

	local filename = Md5(buf) .. '.mdx'
	local model = lib.model.open_by_memory(name, buf)

	print(buf)
	print(model)
	if model == nil then 
		return 
	end 

end

-- 仅保留目标范围的装饰物
local save_area_doo = function ()
	local path = ('%s\\map\\war3map.doo'):format(INPUT_PATH)
	
	local doo = Doodads.new( path )
	doo:load()

	local minx = 27300
	local miny = -999999
	local maxx = 999999
	local maxy = -20500
	
	local new_normal_doos = {}
	for _, obj in ipairs(doo.normal_doos) do
		local x = obj.position[1]
		local y = obj.position[2]
		if x>= minx and y>=miny and x<=maxx and y<= maxy then
			new_normal_doos[#new_normal_doos+1] = obj
		end
	end

	doo.normal_doos = new_normal_doos
	local buffer = doo:save()
	local file = io.open( ('%s\\output\\war3map.doo'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()
end

-- 对称处理部分doo
local duichengchuli_doo = function ()
	local path = ('%s\\map\\war3map.doo'):format(INPUT_PATH)
	
	local doo = Doodads.new( path )
	doo:load()

	local symmetry_x = -9.5*512
	local symmetry_y = 9.5*512

	local minx = -8192
	local miny = 0
	local maxx = 0
	local maxy = 8192

	local ids = {
		['D13B'] = true,
		['D13C'] = true,
		['D10U'] = true,
		['D10V'] = true,
		['D11E'] = true,
		['D10T'] = true,
	}

	local all_normal_doos = {}
	for _, obj in ipairs(doo.normal_doos) do
		local x = obj.position[1]
		local y = obj.position[2]
		if x>= minx and y>=miny and x<=maxx and y<= maxy then
			if obj.id:sub(1, 2) ~= 'D0' or ids[obj.id] then
			end
			all_normal_doos[#all_normal_doos+1] = table.deepcopy(obj)
		end
	end

	-- 右上角
	for _, obj in ipairs(all_normal_doos) do
		local _obj = table.deepcopy(obj)
		_obj.position[1] = Utility.point_vertical_symmetry(_obj.position[1], symmetry_x)
		_obj.angle = Utility.radian_y_vertical( _obj.angle )

		doo.normal_doos[#doo.normal_doos+1] = _obj
	end

	-- 左下角
	for _, obj in ipairs(all_normal_doos) do
		local _obj = table.deepcopy(obj)
		_obj.position[2] = Utility.point_vertical_symmetry(_obj.position[2], symmetry_y)
		_obj.angle = Utility.radian_x_vertical( _obj.angle )

		doo.normal_doos[#doo.normal_doos+1] = _obj
	end

	-- 右下角
	for _, obj in ipairs(all_normal_doos) do
		local _obj = table.deepcopy(obj)
		_obj.position[1] = Utility.point_vertical_symmetry(_obj.position[1], symmetry_x)
		_obj.position[2] = Utility.point_vertical_symmetry(_obj.position[2], symmetry_y)
		_obj.angle = Utility.radian_y_vertical( _obj.angle )
		_obj.angle = Utility.radian_x_vertical( _obj.angle )

		doo.normal_doos[#doo.normal_doos+1] = _obj
	end
	
	
	local buffer = doo:save()
	local file = io.open( ('%s\\output\\war3map.doo'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()

end

-- 对范围内的doo平移到另一个位置
local translation_doo = function ()
	
	local path = ('%s\\map\\war3map.doo'):format([[C:\war3\the-ninth-project]])

	local doo = Doodads.new( path )
	doo:load()

	local minx = -8192
	local miny = 0
	local maxx = 0
	local maxy = 8192

	local all_normal_doos = {}
	for _, obj in ipairs(doo.normal_doos) do
		local x = obj.position[1]
		local y = obj.position[2]
		if x>= minx and y>=miny and x<=maxx and y<= maxy then
			all_normal_doos[#all_normal_doos+1] = table.deepcopy(obj)
		end
	end

	-- 右上角
	for _, obj in ipairs(all_normal_doos) do
		local _obj = table.deepcopy(obj)
		_obj.position[1] = _obj.position[1] + 9.5*512*2

		doo.normal_doos[#doo.normal_doos+1] = _obj
	end

	-- 右下角
	for _, obj in ipairs(all_normal_doos) do
		local _obj = table.deepcopy(obj)
		_obj.position[1] = _obj.position[1] + 9.5*512*2
		_obj.position[2] = _obj.position[2] - 9.5*512*2

		doo.normal_doos[#doo.normal_doos+1] = _obj
	end

	-- 左下角
	for _, obj in ipairs(all_normal_doos) do
		local _obj = table.deepcopy(obj)
		_obj.position[2] = _obj.position[2] - 9.5*512*2

		doo.normal_doos[#doo.normal_doos+1] = _obj
	end

	local buffer = doo:save()
	local file = io.open( ('%s\\output\\war3map.doo'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()
end

-- 移除doo中指定的装饰物
local remove_target_doo = function ()
	local removes = {
		['D005'] = true,
		['D03A'] = true,
		['D10I'] = true,
	}
	
	local path = [[C:\Users\Administrator\Desktop\地形\FYZB\map\war3map.doo]]
	local doo = Doodads.new( path )
	doo:load()

	local new_normal_doos = {}
	for _, obj in ipairs(doo.normal_doos) do
		if not removes[obj.id] then
			new_normal_doos[#new_normal_doos+1] = obj
		end
	end
	
	doo.normal_doos = new_normal_doos
	local buffer = doo:save()
	local file = io.open( ('%s\\output\\war3map.doo'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()
end

-- 修改doo中指定的装饰物
local change_target_doo = function ()

	local path = [[C:\Users\Administrator\Desktop\地形\FYZB\map\war3map.doo]]
	local doo = Doodads.new( path )
	doo:load()

	local ids = {
		['D300'] = 'D30F',
	}
	local scales = {
		['D30F'] = {1,1,1}
	}

	local new_normal_doos = {}
	for _, obj in ipairs(doo.normal_doos) do
		local new_obj = table.deepcopy(obj)
		local new_id
		if ids[obj.id] then
			new_id = ids[obj.id]
			if scales[new_id] then
				new_obj.scale = scales[new_id]
			end
		else
			new_id = obj.id
		end
		ids[obj.id] = new_id

		new_obj.id = new_id
		new_normal_doos[#new_normal_doos+1] = new_obj
	end
	
	doo.normal_doos = new_normal_doos
	local buffer = doo:save()
	local file = io.open( ('%s\\output\\war3map.doo'):format(CURRENT_PATH), 'wb' )
	file:write(buffer)
	file:close()
end

-- 给doo里面，按照规则塞装饰物
local add_target_doo = function ()
	local path = ('%s\\war3map.doo'):format([[C:\Users\Administrator\Desktop\地形]])
	
	local doo = Doodads.new( path )
	doo:load()

	local path2 = ('%s\\war3map.doo'):format([[C:\war3\the-sixth-project\map]])
	local doo2 = Doodads.new( path2 )
	doo2:load()

	for k, v in ipairs(doo2.normal_doos) do
		if v.id == 'D023' then
			local obj = table.deepcopy(v)
			obj.id = 'D423'
			obj.position[3] = -128

			-- for _k, _v in pairs(v) do
			-- 	if _k == 'position' then
			-- 		print(_k, _v)
			-- 		if type(_v) == 'table' then
						
			-- 			for __k, __v in pairs(_v) do
			-- 				print(__k, __v)
			-- 			end
			-- 		end
			-- 	end
			-- end
			
			doo.normal_doos[#doo.normal_doos+1] = obj
		end
	end


	local buffer = doo:save()
	local file = io.open( ('%s\\war3map.doo'):format([[C:\Users\Administrator\Desktop\地形\FYZB\map]]), 'wb' )
	file:write(buffer)
	file:close()
end

-- change_doo_id()